﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Components;
using Divelements.SandDock;
using NokolaNavi;

namespace WorkflowDesigner
{
    public partial class MainPage : UserControl
    {
        private int newDocumentSeed = 1;

        public MainPage()
        {
            InitializeComponent();
            this.ToolboxNavi.DragToolItem += new OnDragToolItem(ToolboxNavi_DragToolItem);
            this.SolutionNavi.LocalWorkflowTreeNodeSelected+=new OnLocalWorkflowTreeNodeSelected(SolutionNavi_WorkflowTreeNodeSelected);
            this.SolutionNavi.ServerWorkflowTreeNodeSelected += new OnServerWorkflowTreeNodeSelected(SolutionNavi_ServerWorkflowTreeNodeSelected);
        }

        
        void ToolboxNavi_DragToolItem(WorkflowComponentBase userObj)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if(wfDoc !=null)
            {
                wfDoc.DraggedObject = userObj;
            }
        }

        void SolutionNavi_WorkflowTreeNodeSelected(string workflowName)
        {
            this.OpenWorkflow(workflowName);
        }

        void SolutionNavi_ServerWorkflowTreeNodeSelected(string workflowName)
        {
            this.OpenPublishedWorkflow(workflowName);
        }

        void wfDoc_WorkflowDesignSelected(WorkflowDesign wfDesign)
        {
            this.propGrid.SelectedObject = wfDesign;
        }

        void wfDoc_NodeSelected(Node node)
        {
            this.propGrid.SelectedObject = node.UserObject;
        }

        #region menu event handlers
        private WorkflowDocument GetActiveWorkflowDocument()
        {
            if (this.DocWindowGroup.SelectedWindow!=null && 
                this.DocWindowGroup.SelectedWindow is DocumentWindow)
            {
                DocumentWindow tabDoc = (DocumentWindow)this.DocWindowGroup.SelectedWindow;
                if (tabDoc.Visibility == Visibility.Visible && tabDoc.IsHitTestVisible)
                {
                    WorkflowDocument wfDoc = (WorkflowDocument)tabDoc.Content;
                    return wfDoc;
                }
            }

            if (this.DockSite.LastActiveWindow !=null && this.DockSite.LastActiveWindow is DocumentWindow)
            {
                DocumentWindow tabDoc = (DocumentWindow)this.DockSite.LastActiveWindow;
                if (tabDoc.Visibility == Visibility.Visible && tabDoc.IsHitTestVisible)
                {
                    WorkflowDocument wfDoc = (WorkflowDocument)tabDoc.Content;
                    return wfDoc;
                }
            }
            return null;
        }

        void wfDoc_ReleaseToolItem()
        {
            this.ToolboxNavi.DraggedObject = null;
        }

        private void NewDocument_Clicked(object sender, MouseButtonEventArgs e)
        {
            DocumentWindow newTabDoc = new DocumentWindow(this.DockSite, "New workflow " + newDocumentSeed);
            // newTabDoc.Dock(WindowOpenMethod.OpenSelectActivate);
            WorkflowDocument wfDoc = new WorkflowDocument();
            wfDoc.NodeSelected += new OnNodeSelected(wfDoc_NodeSelected);
            wfDoc.WorkflowDesignSelected += new OnWorkflowDesignSelected(wfDoc_WorkflowDesignSelected);
            wfDoc.ReleaseToolItem += new OnReleaseToolItem(wfDoc_ReleaseToolItem);
            newTabDoc.Content = wfDoc;
            newTabDoc.Open();
            newDocumentSeed++;
        }

        private void OpenDocument_Clicked(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Workflow files (*.wox)|*wox|All files(*.*)|*.*";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == true)
            {
                WorkflowDesign wfDesign = WorkflowDesign.Read(dialog.File);
                WorkflowFALC.SaveWorkflow(wfDesign);
            }
        }

        private void PublishDocument_Clicked(object sender, MouseButtonEventArgs e)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if (wfDoc != null)
            {
                wfDoc.Publish();
            }
        }

        private void SaveDocument_Clicked(object sender, MouseButtonEventArgs e)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if (wfDoc != null)
                wfDoc.Save();
        }

        private void TestWorkflow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if (wfDoc != null)
                wfDoc.TestWorkflow();
        }

        private void PrintWorkflow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if (wfDoc != null)
                wfDoc.PrintWorkflow();
        }

        private void OpenWorkflow(string workflowName)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if(wfDoc !=null)
            {
                wfDoc.NodeSelected += new OnNodeSelected(wfDoc_NodeSelected);
                wfDoc.WorkflowDesignSelected += new OnWorkflowDesignSelected(wfDoc_WorkflowDesignSelected);
                wfDoc.ReleaseToolItem += new OnReleaseToolItem(wfDoc_ReleaseToolItem);
                wfDoc.OpenLocalWorkflow(workflowName);
                DocumentWindow tabDoc = (DocumentWindow) wfDoc.Parent;
                tabDoc.Title = workflowName;
            }
            else
            {
                // open in new tabbed doc
                DocumentWindow newTabDoc = new DocumentWindow(this.DockSite, "New workflow " + newDocumentSeed);
                WorkflowDocument wfDoc2 = new WorkflowDocument();
                wfDoc2.NodeSelected += new OnNodeSelected(wfDoc_NodeSelected);
                wfDoc2.WorkflowDesignSelected += new OnWorkflowDesignSelected(wfDoc_WorkflowDesignSelected);
                wfDoc2.ReleaseToolItem += new OnReleaseToolItem(wfDoc_ReleaseToolItem);
                newTabDoc.Content = wfDoc2;
                newTabDoc.Open();
                newDocumentSeed++;
                wfDoc2.OpenLocalWorkflow(workflowName);
                newTabDoc.Title = workflowName;
            }
        }

        private void OpenPublishedWorkflow(string workflowName)
        {
            WorkflowDocument wfDoc = this.GetActiveWorkflowDocument();
            if (wfDoc != null)
            {
                wfDoc.NodeSelected += new OnNodeSelected(wfDoc_NodeSelected);
                wfDoc.WorkflowDesignSelected += new OnWorkflowDesignSelected(wfDoc_WorkflowDesignSelected);
                wfDoc.ReleaseToolItem += new OnReleaseToolItem(wfDoc_ReleaseToolItem);
                wfDoc.OpenServerWorkflow(workflowName);
                DocumentWindow tabDoc = (DocumentWindow)wfDoc.Parent;
                tabDoc.Title = workflowName;
            }
            else
            {
                // open in new tabbed doc
                DocumentWindow newTabDoc = new DocumentWindow(this.DockSite, "New workflow " + newDocumentSeed);
                WorkflowDocument wfDoc2 = new WorkflowDocument();
                wfDoc2.NodeSelected += new OnNodeSelected(wfDoc_NodeSelected);
                wfDoc2.WorkflowDesignSelected += new OnWorkflowDesignSelected(wfDoc_WorkflowDesignSelected);
                wfDoc2.ReleaseToolItem += new OnReleaseToolItem(wfDoc_ReleaseToolItem);
                newTabDoc.Content = wfDoc2;
                newTabDoc.Open();
                newDocumentSeed++;
                wfDoc2.OpenServerWorkflow(workflowName);
                newTabDoc.Title = workflowName;
            }
        }
        #endregion

        #region animation on menu
        private void ButtonSeven_MouseMove(object sender, MouseEventArgs e)
        {
            NaviButton button = sender as NaviButton;
            if (button == null) return;
            textInfo.Text = button.Title;
        }
        #endregion

        
    }
}
